package com.my.magicApi.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class ExcelUtil {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    public static void createTitle(HSSFSheet sheet, String[] title, HSSFCellStyle cellStyle) {
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<title.length;i++){
            if (i == 6){
                sheet.setColumnWidth(i,8000);
            }else if (i == 4 || i == 7){
                sheet.setColumnWidth(i,6000);
            }else {
                sheet.setColumnWidth(i,4000);
            }
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(cellStyle);
        }
    }

    public static void createExcelTitle(HSSFSheet sheet, JSONArray title, HSSFCellStyle cellStyle) {
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<title.size();i++){
            JSONObject titleObj = title.getJSONObject(i);
            sheet.setColumnWidth(i,titleObj.getInteger("width"));
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(titleObj.getString("title"));
            cell.setCellStyle(cellStyle);
        }
    }

    public static void setRowValue(HSSFSheet sheet, List list, HSSFCellStyle valueStyle) {

        for(int i=0;i<list.size();i++){
            AtomicInteger index = new AtomicInteger(0);
            HSSFRow row = sheet.createRow(i + 1);
            JSONObject jsonObject = (JSONObject)JSONObject.toJSON(list.get(i));
            HSSFCell cell0 = row.createCell(index.get());
            cell0.setCellValue(jsonObject.getString("name"));
            cell0.setCellStyle(valueStyle);
            HSSFCell cell1 = row.createCell(index.incrementAndGet());
            cell1.setCellValue(jsonObject.getString("sex"));
            cell1.setCellStyle(valueStyle);
            HSSFCell cell2 = row.createCell(index.incrementAndGet());
            cell2.setCellValue(jsonObject.getString("age"));
            cell2.setCellStyle(valueStyle);
            HSSFCell cell3 = row.createCell(index.incrementAndGet());
            cell3.setCellValue(jsonObject.getString("mobile"));
            cell3.setCellStyle(valueStyle);
            HSSFCell cell4 = row.createCell(index.incrementAndGet());
            cell4.setCellValue(jsonObject.getString("cardNo"));
            cell4.setCellStyle(valueStyle);
            HSSFCell cell5 = row.createCell(index.incrementAndGet());
            cell5.setCellValue(jsonObject.getInteger("testResult") == 0?"阴性":"阳性");
            cell5.setCellStyle(valueStyle);
            HSSFCell cell6 = row.createCell(index.incrementAndGet());
            cell6.setCellValue(jsonObject.getString("district")+jsonObject.getString("detailedAddress"));
            cell6.setCellStyle(valueStyle);
            HSSFCell cell7 = row.createCell(index.incrementAndGet());
            cell7.setCellValue(dateFormat.format(jsonObject.getDate("createTimeText")));
            cell7.setCellStyle(valueStyle);
        }
    }

    public static void setExcelRowValue(HSSFSheet sheet, List list, HSSFCellStyle valueStyle, JSONArray field) throws Exception{
        for(int i=0;i<list.size();i++){
            AtomicInteger index = new AtomicInteger(-1);
            HSSFRow row = sheet.createRow(i + 1);
            JSONObject jsonObject = (JSONObject)JSONObject.toJSON(list.get(i));
            for (int j=0;j<field.size();j++){
                HSSFCell cell = row.createCell(index.incrementAndGet());
                JSONObject fieldInfo = field.getJSONObject(j);
                String fieldName = fieldInfo.getString("fieldName");
                String fieldType = fieldInfo.getString("type");
                if ("Date".equals(fieldType)){
                    String format = fieldInfo.getString("format");
                    SimpleDateFormat dateFormat = new SimpleDateFormat(format);
                    cell.setCellValue(dateFormat.format(jsonObject.getDate(fieldName)));
                }else if ("Integer".equals(fieldType)){
                    cell.setCellValue(jsonObject.getInteger(fieldName));
                }else if ("Long".equals(fieldType)){
                    cell.setCellValue(jsonObject.getLong(fieldName));
                }else {
                    cell.setCellValue(jsonObject.getString(fieldName));
                }
                cell.setCellStyle(valueStyle);
            }
        }
    }
}
